package com.uchedao.vehicledetection.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.JsonObject;
import com.uchedao.mybaselibrary.entity.VersionInfo;
import com.uchedao.vehicledetection.R;
import com.uchedao.vehicledetection.config.Api;
import com.uchedao.vehicledetection.config.AppInfoManager;
import com.uchedao.vehicledetection.config.UserInfoManager;
import com.uchedao.vehicledetection.network.HttpRequest;
import com.uchedao.vehicledetection.utils.ProgressDialogUtil;
import com.uchedao.vehicledetection.view.carlist.db.CarDbHelper;

import java.util.HashMap;
import java.util.Map;

public class LoginActivity extends CJDBaseActivity {

    /**
     * 测试账号：13960864746 123123
     */

    private EditText edAccount;
    private EditText edPwd;
    private Button btnLogin;
    private VersionInfo info;

    @Override
    protected String getActivityTag() {
        return "LoginActivity";
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        edAccount = (EditText) findViewById(R.id.activity_login_username_tv);
        edPwd = (EditText) findViewById(R.id.activity_login_password_et);
        btnLogin = (Button) findViewById(R.id.activity_login_commit_btn);
    }

    @Override
    protected void initData() {
        AppInfoManager.syncToken();

        setTitle("登陆");
        findViewById(R.id.title_right_iv).setVisibility(View.GONE);
        CarDbHelper.importDatabase(LoginActivity.this);

//        edAccount.setText("13960864746");
//        edPwd.setText("123123");
    }

    @Override
    protected void initListener() {
        //忘记密码
        findViewById(R.id.textView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginActivity.this, ResetPwdActivity.class));
            }
        });

        findViewById(R.id.tvLoginRegist).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginActivity.this, RegistActivity.class));
            }
        });

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                login(edAccount.getText().toString(), edPwd.getText().toString());
            }
        });
    }

    public void login(final String mobile, final String password) {
        ProgressDialogUtil.showProgressDlg(this, "登录中");
        Map<String, String> map = new HashMap<String, String>();
        map.put("mobile", mobile);
        map.put("password", password);
        map.put("app_name", "checker");
        map.put("registration_id", "");
        Request request = HttpRequest.getRequest(HttpRequest.POST, Api.Action.USER_LOGIN, map
                , new Response.Listener<JsonObject>() {
            @Override
            public void onResponse(JsonObject response) {
                ProgressDialogUtil.dismissProgressDlg();
                UserInfoManager.savePASS_PORT(response.get("passport").getAsString());
                UserInfoManager.saveUSERNAME(response.get("name").getAsString());
                UserInfoManager.saveCity(response.get(UserInfoManager.CITY).getAsString());
                UserInfoManager.saveLevel(response.get(UserInfoManager.LEVEL).getAsString());
                UserInfoManager.saveCompany(response.get(UserInfoManager.COMPANY).getAsString());
                UserInfoManager.saveMOBILE(mobile);
                UserInfoManager.savePASSWORD(password);
                UserInfoManager.saveSTATE(true);

                startActivity(new Intent(LoginActivity.this, CheckCarActivity.class));
                finish();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                ProgressDialogUtil.dismissProgressDlg();
                Toast.makeText(LoginActivity.this, error.getMessage() + "", Toast.LENGTH_SHORT).show();
            }
        }, false);
        addQueue(request);
    }
}
